package com.fmblzf.library.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

/**
 * Created by Administrator on 2015/10/23.
 */
public class LineView extends android.view.View  {

    //画线的画笔
    private Paint linePaint;
    //画背景的画笔
    private Paint backPaint;
    //线的长度
    private int lineLenght;
    //比例 画线的长度占总长度的比例
    private int rate = 4;
    //当前状态
    private int curStatus = 0;


    public LineView(Context context) {
        this(context, null, 0);
    }

    public LineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initWidget();
    }

    /**
     * 初始化控件
     */
    private void initWidget() {

        backPaint = new Paint();
        backPaint.setAntiAlias(true);
        backPaint.setColor(Color.parseColor("#eeeeee"));
//        backPaint.setColor(Color.RED);

        linePaint = new Paint();
        linePaint.setColor(Color.parseColor("#579c31"));
        linePaint.setAntiAlias(true);

        //设置背景透明
        setBackgroundColor(Color.parseColor("#00000000"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {

        //
        backPaint.setStrokeWidth((getMeasuredHeight() < getMeasuredWidth() ? getMeasuredHeight() : getMeasuredWidth()) / 2);
        canvas.drawLine(0, getMeasuredHeight() * 3 / 4f, getMeasuredWidth(), getMeasuredHeight() * 3 / 4f, backPaint);
        //canvas.restore();

        if(lineLenght == 0){
            lineLenght = (getHeight()>getWidth()?getHeight():getWidth())/4;
        }
        linePaint.setStrokeWidth((getMeasuredHeight() < getMeasuredWidth() ? getMeasuredHeight() : getMeasuredWidth())+4);
        canvas.drawLine(curStatus * lineLenght, getMeasuredHeight()/2, lineLenght * (curStatus+1), getMeasuredHeight()/2, linePaint);
        canvas.restore();
    }

    /**
     * 获取画线的当前状态
     * @return
     */
    public int getCurStatus() {
        return curStatus;
    }

    /**
     * 设置当前画线的状态
     * @param curStatus
     */
    public void setCurStatus(int curStatus) {
        this.curStatus = curStatus;
        invalidate();
    }

}
